<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.10.2.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/laydate/laydate.js"></script>
<script src="<?php echo URL::base() ?>media/layer/layer.js"></script>
<div class="container-fluid">
    <form class="form-horizontal" method='get' action='<?php echo URL::site()?>checkpurchaseorder/returnApplication'>
        <div class="control-group">
            <label class="control-label">供应商:</label>
            <div class="controls">
                <input type="text" class="input2" name="supplier_name" id="supplier_name" value="<?php echo isset($_GET['supplier_name'])?$_GET['supplier_name']:''?>">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">SKU:</label>
            <div class="controls">
                <input type="text" class="input2" name="sku" value="<?php echo isset($_GET['sku'])?$_GET['sku']:''?>">
            </div>
        </div>
         <div class="control-group">
            <label class="control-label">仓库:</label>
            <div class="controls">
                <select name="store" class="input2">
                    <option value="">选择仓库</option>
                    <?php foreach ($warehouses as $key =>$val){?>
                        <option value="<?php echo $key?>" <?php echo $_GET['store']==$key?'selected="selected"':'';?>><?php echo $val?></option>
                    <?php }?>
                </select>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">主体:</label>
            <div class="controls">
                <select name="enterprise_dominant" class="input2">
                    <option value="">选择主体</option>
                    <?php foreach ($enterprise_dominant as $key =>$val){?>
                    <option value="<?php echo $key?>" <?php echo $_GET['enterprise_dominant']==$key?'selected="selected"':'';?>><?php echo $val?></option>
                    <?php }?>
                </select>
            </div>
        </div>
        <div class="control-group">

            <div class="controls">

                <input class="btn btn-info" type="submit" name="sub" value="查询"/>
                <button class="btn" type="button" name="sub"  onclick="resetdata()">重置</button>
<!--                <input class="btn btn-info" type="submit" name="down" id="downbtn" value="导出"/>-->
            </div>
        </div>
    </form>
    <table class="table table-hover ">
        <thead>
        <tr>
            <th><input type="checkbox" name="checkboxall" onclick="checkall()"></th>
            <th>供应商</th>
            <th>SKU</th>
            <th>中文名称</th>
            <th>仓库</th>
            <th>主体</th>
            <th>库存量</th>
            <th>退货量</th>
            <th>备注</th>
            <th>操作</th>
        </tr>
        </thead>
        <?php foreach ($result as $k=>$v){ ?>
        <tr>
            <td class="supplier_id"><input type="checkbox" name="checkbox" value="<?php echo $v['supplier_id']?>"></td>
            <td class="supplier_name"><?php echo $v['supplier_name']?></td>
            <td class="sku"><?php echo $v['sku']?></td>
            <td class="productname"><?php echo $v['productname']?></td>
            <td class="warehouseid"><input type="hidden" name="warehouseid" value="<?php echo $v['warehouseid']?>"><?php echo $v['warehouseid']==1?'深圳仓':'苏州仓'?></td>
            <td class="enterprise_dominant"><input type="hidden" name="enterprise_dominant" value="<?php echo $v['enterprise_dominant']?>"><?php echo $enterprise_dominant[$v['enterprise_dominant']]; ?></td>
            <td class="sku_qty"><?php echo $v['qty'];?></td>
            <td class="return_qty"><input type="text" style="width: 50px;" name="return_qty" onkeyup="clearNoNum(this)"></td>
            <td class="remark"><input type="text" style="width: 100px;" name="remark"></td>
            <td>
                <button class="btn btn-success" type="button" onclick="saveOne(this)">提交</button>
            </td>
        </tr>
        <?php }?>

    </table>
    <?php if (isset($pagination)) {
        echo $pagination;
    } ?>
    <div class="control-group">
        <a class="btn btn-info" onclick="saveAll()">批量提交</a>
    </div>
</div>

<script type="text/javascript">
    function resetdata() {
        $(".input-medium").val("");
        $(".input-xlarge").val("");
        $(".input2").val("");
//        $("#status").val("1");
    }
    function checkall() {
        $('input[name=checkbox]').each(function () {
            if(this.checked){
                $(this).prop('checked',false);
            }else{

                $(this).prop('checked',true);
            }
        });
    }
    function saveOne(obj) {
        var remark = $(obj).parent('td').siblings('.remark').find('input[name=remark]').val();
        var return_qty = $(obj).parent('td').siblings('.return_qty').find('input[name=return_qty]').val();
        var enterprise_dominant = $(obj).parent('td').siblings('.enterprise_dominant').find('input[name=enterprise_dominant]').val();
        var warehouseid = $(obj).parent('td').siblings('.warehouseid').find('input[name=warehouseid]').val();
        var sku = $(obj).parent('td').siblings('.sku').text();
        var sku_name = $(obj).parent('td').siblings('.productname').text();
        var sku_qty = $(obj).parent('td').siblings('.sku_qty').text();
        var supplier_name = $(obj).parent('td').siblings('.supplier_name').text();
        var supplier_id = $(obj).parent('td').siblings('.supplier_id').find('input[name=checkbox]').val();
        var arr = {
                'remark':remark,
                'return_qty':return_qty,
                'enterprise_dominant':enterprise_dominant,
                'warehouseid':warehouseid,
                'sku':sku,
                'sku_name':sku_name,
                'supplier_name':supplier_name,
                'supplier_id':supplier_id
            };
        if(return_qty=='' || parseInt(return_qty)<=0 || parseInt(return_qty)> parseInt(sku_qty)){
            layer.alert('退货量输入有误', {icon: 5});
            return false;
        }
        layer.confirm('你确定要提交吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.post("<?php echo URL::base() ?>checkpurchaseorder/saveOne",
                {arr: arr},
                function (data) {
                    layer.alert(data, {closeBtn: 0},function () {
                        window.location.reload();
                    });
                });
        }, function(){
        });

    }
    //批量提交
    function saveAll() {
        var arr = [],msg='';
        $('input[name=checkbox]:checked').each(function (i) {
            var remark = $(this).parent('td').siblings('.remark').find('input[name=remark]').val();
            var return_qty = $(this).parent('td').siblings('.return_qty').find('input[name=return_qty]').val();
            var enterprise_dominant = $(this).parent('td').siblings('.enterprise_dominant').find('input[name=enterprise_dominant]').val();
            var warehouseid = $(this).parent('td').siblings('.warehouseid').find('input[name=warehouseid]').val();
            var sku = $(this).parent('td').siblings('.sku').text();
            var sku_name = $(this).parent('td').siblings('.productname').text();
            var sku_qty = $(this).parent('td').siblings('.sku_qty').text();
            var supplier_name = $(this).parent('td').siblings('.supplier_name').text();
            var supplier_id = $(this).val();
            if(return_qty=='' || parseInt(return_qty)<=0 || parseInt(return_qty)> parseInt(sku_qty)){
                msg += '请输入SKU:'+sku+'的正确退货量\n';
            }
            arr[i] = {
                'remark':$.trim(remark),
                'return_qty':$.trim(return_qty),
                'enterprise_dominant':$.trim(enterprise_dominant),
                'warehouseid':$.trim(warehouseid),
                'sku':$.trim(sku),
                'sku_name':$.trim(sku_name),
                'supplier_name':$.trim(supplier_name),
                'supplier_id':$.trim(supplier_id)
            };
        });
        if(msg!=''){
            layer.alert(msg, {icon: 5});
            return false;
        }
        if(arr.length==0){
            layer.alert('请选择数据后提交', {icon: 5});
            return false;
        }else{
            layer.confirm('你确定要提交吗？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                $.post("<?php echo URL::base() ?>checkpurchaseorder/saveAll",
                    {arr: arr},
                    function (data) {
                        layer.alert(data, {closeBtn: 0},function () {
                            window.location.reload();
                        });
                    });
            }, function(){
            });

        }

    }
    function clearNoNum(obj) {
        obj.value = obj.value.replace(/[^0-9]/g, "");  //清除“数字”和“.”以外的字符

        obj.value = obj.value.replace(/^0/g, "");  //验证第一个字符是数字而不是.
    }
</script>
